package com.etc.traval.daoimpl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.List;

import com.etc.traval.dao.ComplaintDao;
import com.etc.traval.util.JDBCUtil;
import com.etc.travel.entity.Complaint;
import com.etc.travel.entity.User;

import oracle.jdbc.internal.OracleTypes;

public class ComplaintDaoImpl implements ComplaintDao{

	JDBCUtil util = new JDBCUtil();
	
	/**
	 * 添加投诉的方法
	 * @author cjw
	 * @param complaint 要添加的投诉内容
	 * @return i>0 添加行数是否大于0
	 */
	@Override
	public boolean addComplaint(Complaint complaint) {
		List<Integer> list=new ArrayList<Integer>();
		list.add(Types.INTEGER);
		Object[] obj = util.callHasReturn("{call proc_addcomplaint(?,?,?,?,?)}",
				list,complaint.getComplaint_user().getUser_id(),complaint.getComplaint_time(),
				complaint.getComplaint_content(),complaint.getComplaint_status());	
		util.closeAll();
		int i = (Integer) obj[0];		
		return i>0;
	}

	
	/**
	 * 根据ID 将未审核投诉改为已审核投诉
	 * @author cjw
	 * @param ID 要修改的投诉的ID
	 * @return i>0  执行行数是否大于0
	 */
	@Override
	public boolean updateCompStatusById(int id) {
		List<Integer> list=new ArrayList<Integer>();
		list.add(Types.INTEGER);
		Object[] obj = util.callHasReturn("call proc_updateCompStatusById(?,?)", list, id);
		util.closeAll();
		int i = (Integer) obj[0];		
		return i>0;
	}


	/**
	 * 根据ID删除投诉
	 * @author cjw
	 * @param id 要删除的投诉的ID
	 * @return 返回执行行数是否大于0
	 */
	@Override
	public boolean deleteCompById(int id) {
		List<Integer> list=new ArrayList<Integer>();
		list.add(Types.INTEGER);
		Object[] obj = util.callHasReturn("call proc_deleteCompById(?,?)", list, id);
		util.closeAll();
		int i = (Integer) obj[0];		
		return i>0;
	}


	/**
	 * 分页查询所有投诉
	 * @author cjw
	 * @param size 分页每页条数
	 * @param pages 分页第几页
	 * @return list2 返回对应页数的投诉集合
	 */
	@Override
	public List<Complaint> queryCompByPage(int size, int pages) {
		List<Complaint> list2 = new ArrayList<Complaint>();
		List<Integer> list = new ArrayList<Integer>();
		list.add(OracleTypes.CURSOR);
		Object[] obj = util.callHasReturn("{call proc_queryCompByPage(?,?,?)}", list,size,pages);
		ResultSet rs = (ResultSet) obj[0];
		try {
			while (rs.next()) {
				Complaint complaint = new Complaint();
				complaint.setComplaint_id(rs.getInt("complaint_id"));
				complaint.setComplaint_content(rs.getString("complaint_content"));
				complaint.setComplaint_status(rs.getString("complaint_status"));
				complaint.setComplaint_time(rs.getTimestamp("complaint_time"));
				List<Integer> list1=new ArrayList<Integer>();
				list1.add(OracleTypes.CURSOR);
				Object[] obj1  =  util.callHasReturn("{call  proc_queryuserbyid(?,?)}", list1, rs.getInt("complaint_user"));
				ResultSet rs1 = (ResultSet) obj1[0];
				User user= null;
					if(rs1.next()){
						user = new User();
						user.setUser_id(rs1.getInt("user_id"));
						user.setUser_name(rs1.getString("user_name"));
						user.setUser_pwd(rs1.getString("user_pwd"));
						user.setUser_sex(rs1.getString("user_sex"));
						user.setUser_tel(rs1.getString("user_tel"));
						user.setUser_logo(rs1.getString("user_logo"));
						user.setUser_money(rs1.getDouble("user_money"));
						user.setUser_status(rs1.getString("user_status"));
					}
				complaint.setComplaint_user(user);
				list2.add(complaint);
				rs1.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				rs.close();
				util.closeAll();
			} catch (Exception e2) {
			}
		}	
		return list2;
	}


	/**
	 * 根据ID查投诉
	 * @author cjw
	 * @param 要查的投诉ID
	 * @return complaint 查到的投诉信息
	 */
	@Override
	public Complaint queryComplaintById(int id) {
		List<Integer> list = new ArrayList<Integer>();
		list.add(OracleTypes.CURSOR);
		Object[] obj = util.callHasReturn("{call proc_queryComplaintById(?,?)}", list,id);
		ResultSet rs = (ResultSet) obj[0];
		Complaint complaint=null;
		try {
			if (rs.next()) {
				complaint = new Complaint();
				complaint.setComplaint_id(rs.getInt("complaint_id"));
				complaint.setComplaint_content(rs.getString("complaint_content"));
				complaint.setComplaint_status(rs.getString("complaint_status"));
				complaint.setComplaint_time(rs.getTimestamp("complaint_time"));
				List<Integer> list1=new ArrayList<Integer>();
				list1.add(OracleTypes.CURSOR);
				Object[] obj1  =  util.callHasReturn("{call  proc_queryuserbyid(?,?)}", list1, rs.getInt("complaint_user"));
				ResultSet rs1 = (ResultSet) obj1[0];
				User user= null;
					if(rs1.next()){
						user = new User();
						user.setUser_id(rs1.getInt("user_id"));
						user.setUser_name(rs1.getString("user_name"));
						user.setUser_pwd(rs1.getString("user_pwd"));
						user.setUser_sex(rs1.getString("user_sex"));
						user.setUser_tel(rs1.getString("user_tel"));
						user.setUser_logo(rs1.getString("user_logo"));
						user.setUser_money(rs1.getDouble("user_money"));
						user.setUser_status(rs1.getString("user_status"));
					}
				complaint.setComplaint_user(user);
				rs1.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				rs.close();
				util.closeAll();
			} catch (Exception e2) {
			}
		}	
		return complaint;
	}


	/**
	 * 根据投诉人的ID 查找相关的投诉信息 并分页显示
	 * @author cjw
	 * @param size 分页每页条数
	 * @param pages 分页页数
	 * @param userid 投诉人的ID
	 * @return list2 返回查询到的信息
	 */
	@Override
	public List<Complaint> queruCompByUserIdAndPage(int size, int pages, int userid) {
		List<Complaint> list2 = new ArrayList<Complaint>();
		List<Integer> list = new ArrayList<Integer>();
		list.add(OracleTypes.CURSOR);
		Object[] obj = util.callHasReturn("{call proc_queryCompByUserId(?,?,?,?)}", list,size,pages,userid);
		ResultSet rs = (ResultSet) obj[0];
		try {
			while (rs.next()) {
				Complaint complaint = new Complaint();
				complaint.setComplaint_id(rs.getInt("complaint_id"));
				complaint.setComplaint_content(rs.getString("complaint_content"));
				complaint.setComplaint_status(rs.getString("complaint_status"));
				complaint.setComplaint_time(rs.getTimestamp("complaint_time"));
				List<Integer> list1=new ArrayList<Integer>();
				list1.add(OracleTypes.CURSOR);
				Object[] obj1  =  util.callHasReturn("{call  proc_queryuserbyid(?,?)}", list1, rs.getInt("complaint_user"));
				ResultSet rs1 = (ResultSet) obj1[0];
				User user= null;
					if(rs1.next()){
						user = new User();
						user.setUser_id(rs1.getInt("user_id"));
						user.setUser_name(rs1.getString("user_name"));
						user.setUser_pwd(rs1.getString("user_pwd"));
						user.setUser_sex(rs1.getString("user_sex"));
						user.setUser_tel(rs1.getString("user_tel"));
						user.setUser_logo(rs1.getString("user_logo"));
						user.setUser_money(rs1.getDouble("user_money"));
						user.setUser_status(rs1.getString("user_status"));
					}
				complaint.setComplaint_user(user);
				list2.add(complaint);
				rs1.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				rs.close();
				util.closeAll();
			} catch (Exception e2) {
			}
		}	
		return list2;
	}

	
}
